import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
import omit from 'omit.js';
import React, { useMemo } from 'react';
import FormItem from '../FormItem';
import { useFormAttrsContext } from '../contexts/FormAttrsContext';
import { useFormContext } from '../contexts/FormContext';
import { DetailComponents } from '../detail';
import { extract } from '../utils';
import { AntdFormItemPropNames, DetailFormItemPropNames } from './consts';
import { enhanceFieldProps, getRealRules } from './utils';
var _excluded = ['children', 'detail', 'detailType', 'DetailComponent'],
  _excluded2 = ['formItemClassName', 'formItemStyle'];
/**
 * 创建 Form.Item 包裹的表单控件，提供详情展示
 * @param FieldComponent
 * @param options
 * @returns
 */
function createField(FieldComponent, options) {
  var Field = function Field(_ref) {
    var children = _ref.children,
      detailProp = _ref.detail,
      detailTypeProp = _ref.detailType,
      DetailComponentProp = _ref.DetailComponent,
      rest = _objectWithoutProperties(_ref, _excluded);
    var form = useFormContext();
    var _useFormAttrsContext = useFormAttrsContext(),
      detail = _useFormAttrsContext.detail;
    var isDetail = useMemo(
      function () {
        return detailProp !== undefined ? detailProp : detail;
      },
      [detail, detailProp],
    );
    var _ref2 = options || {},
      _ref2$defaultDetailTy = _ref2.defaultDetailType,
      defaultDetailType = _ref2$defaultDetailTy === void 0 ? 'text' : _ref2$defaultDetailTy,
      defaultDetailComponent = _ref2.defaultDetailComponent,
      _ref2$defaultFormItem = _ref2.defaultFormItemProps,
      defaultFormItemProps = _ref2$defaultFormItem === void 0 ? {} : _ref2$defaultFormItem,
      getFormItemPropsFromProps = _ref2.getFormItemPropsFromProps;
    var basicFormItemProps = extract(
      rest,
      isDetail ? DetailFormItemPropNames : AntdFormItemPropNames,
    );
    var extraFormItemProps = getFormItemPropsFromProps
      ? getFormItemPropsFromProps(rest, form)
      : undefined;
    var formItemProps = _objectSpread(
      _objectSpread(_objectSpread({}, defaultFormItemProps), basicFormItemProps),
      extraFormItemProps,
    );
    var formItemClassName = formItemProps.formItemClassName,
      formItemStyle = formItemProps.formItemStyle,
      formItemRest = _objectWithoutProperties(formItemProps, _excluded2);
    // @ts-ignore
    var fieldProps = omit(rest, AntdFormItemPropNames);
    var DetailComponent;
    if (DetailComponentProp) {
      DetailComponent = DetailComponentProp;
    } else if (detailTypeProp) {
      DetailComponent = DetailComponents[detailTypeProp];
    } else if (defaultDetailComponent) {
      DetailComponent = defaultDetailComponent;
    } else {
      DetailComponent = DetailComponents[defaultDetailType];
    }
    if (isDetail) {
      return /*#__PURE__*/ React.createElement(
        FormItem,
        _objectSpread(
          {
            className: formItemClassName,
            style: formItemStyle,
          },
          formItemRest,
        ),
        /*#__PURE__*/ React.createElement(
          DetailComponent,
          _objectSpread(
            _objectSpread({}, fieldProps),
            {},
            {
              valuePropName: formItemProps.valuePropName,
            },
          ),
          children,
        ),
      );
    }
    return /*#__PURE__*/ React.createElement(
      FormItem,
      _objectSpread(
        _objectSpread(
          {
            className: formItemClassName,
            style: formItemStyle,
          },
          formItemRest,
        ),
        {},
        {
          rules: getRealRules(formItemRest, form),
        },
      ),
      /*#__PURE__*/ React.createElement(
        FieldComponent,
        _objectSpread({}, enhanceFieldProps(fieldProps, formItemRest.rules, form)),
        children,
      ),
    );
  };
  return Field;
}
function FieldDecorator(options) {
  if (!options) {
    return function (FieldComponent) {
      return createField(FieldComponent, options);
    };
  }
  return function (FieldComponent) {
    return createField(FieldComponent, options);
  };
}
export default createField;
export var Field = FieldDecorator;
